package com.iknight.theory.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iknight.theory.api.TeLineSegService;
import com.iknight.theory.mapper.TeLineSegMapper;
import com.iknight.theory.model.TELineSegModel;
import io.swagger.models.auth.In;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TeLineSegServiceImpl implements TeLineSegService<TELineSegModel, String> {
    private final static Logger logger = LogManager.getLogger(TeLineSegServiceImpl.class);
    @Autowired
    private TeLineSegMapper<TELineSegModel, String> teLineSegMapper;

    @Override
    public void save(TELineSegModel record) {
        if (null == teLineSegMapper.selectOne(record.getSeg_id())) {
            teLineSegMapper.insert(record);
        } else {
            teLineSegMapper.update(record);
        }
    }

    @Override
    public void delete(String pkey) {
        teLineSegMapper.delete(pkey);
    }

    @Override
    public TELineSegModel selectOne(String pkey) {
        TELineSegModel result = teLineSegMapper.selectOne(pkey);
        logger.info("执行查询成功!");
        return result;
    }

    @Override
    public PageInfo<TELineSegModel> selectPage(Map<String, Object> params, int pageStart, int pageSize) {
//        return null;
        PageHelper.startPage(pageStart, pageSize, true);
        List<TELineSegModel> result = teLineSegMapper.selectByCond(params);
        PageInfo<TELineSegModel> page = new PageInfo<>(result);
        logger.info("分页查询成功,当前查询页数为:" + pageStart + ",单页条数为:" + pageSize);
        return page;
    }

    @Override
    public void batchSave(List<TELineSegModel> entities) {
        for (TELineSegModel entity : entities) {
            this.save(entity);
        }
    }

    @Override
    public List<Map> getSegsByLineId(String line_id) {
        return teLineSegMapper.getSegsByLineId(line_id);
    }

    @Override
    public PageInfo<Map> getSegsByParam(Map<String, Object> param) {
        int pageSize = Integer.parseInt(param.get("pagesize").toString());
        int pageStart = Integer.parseInt(param.get("pagestart").toString());
        PageHelper.startPage(pageStart, pageSize, true);
        List<Map> result = teLineSegMapper.getSegsByParam(param);
        PageInfo<Map> pageInfo = new PageInfo<>(result);
        return pageInfo;
    }
}
